import { singleClone, Type } from '@utils/Global'

const PREFIX = 'qa_list_'
const QaListActionType = {
  init_qa: PREFIX + 'init_qa',
  insert_qa: PREFIX + 'insert_qa',
  update_voice_status: PREFIX + 'update_voice_status',
  stop_all_voice: PREFIX + 'stop_all_voice',
  do_self_like_item: `${PREFIX}do_self_like_item`,
  get_qa_question_answer_up_msg: `${PREFIX}get_qa_question_answer_up_msg`,
  get_qa_question_answer_msg: `${PREFIX}get_qa_question_answer_msg`,
  get_system_qa_msg: `${PREFIX}get_system_qa_msg`,
  get_remote_self_qa_text_msg: `${PREFIX}get_remote_self_qa_text_msg`,
  get_transfer_question_success_msg: `${PREFIX}get_transfer_question_success_msg`
}

const initQa = (list) => {
  return {
    type: QaListActionType.init_qa,
    list: list
  }
}
const insertQa = (item) => {
  return {
    type: QaListActionType.insert_qa,
    item: item
  }
}

const getSystemQaMsg = (msg) => {
  return {
    type: QaListActionType.get_system_qa_msg,
    msg
  }
}
const updateVoiceStatus = (payload) => {
  return {
    type: QaListActionType.update_voice_status,
    payload
  }
}
const stopAllVoice = () => {
  return {
    type: QaListActionType.stop_all_voice
  }
}

function doSelfLikeItem({ msgType, id }) {
  return {
    type: QaListActionType.do_self_like_item,
    payload: { msgType, id }
  }
}

function getQaQuestionOrAnswerUpMsg({ msgType, msg, isRemoteSelf, isSelf }) {
  return {
    type: QaListActionType.get_qa_question_answer_up_msg,
    payload: { msgType, msg, isRemoteSelf, isSelf }
  }
}
function getQaQuestionOrAnswerMsg({ msgType, msg, isRemoteSelf, isSelf }) {
  return (dispatch, getState) => {
    let _msg = singleClone(msg)
    if (_msg.type === Type.COMMON_TYPE.COMMON_QA_ANSWER) {
      _msg.data.question = getState().QuestionState.questionList.find(r => r.id === _msg.data.answer.question_id)
    }
    dispatch({
      type: QaListActionType.get_qa_question_answer_msg,
      payload: { msgType, msg: _msg, isRemoteSelf, isSelf }
    })
  }
}

function getRemoteSelfQaTextMsg({msg}) {
  return {
    type: QaListActionType.get_remote_self_qa_text_msg,
    payload: {msg}
  }
}
function getTransferQuestionSuccessMsg({msg}) {
  return {
    type: QaListActionType.get_transfer_question_success_msg,
    payload: {msg}
  }
}

module.exports = {
  QaListActionType,
  initQa,
  insertQa,
  updateVoiceStatus,
  stopAllVoice,
  doSelfLikeItem,
  getQaQuestionOrAnswerUpMsg,
  getQaQuestionOrAnswerMsg,
  getSystemQaMsg,
  getRemoteSelfQaTextMsg,
  getTransferQuestionSuccessMsg
}
